package com.hyjx.framework.common.interceptor;


import com.hyjx.framework.common.Constants;
import com.hyjx.framework.common.PropertyUtil;
import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.login.LoginService;
import com.hyjx.framework.service.CacheService;
import com.hyjx.framework.service.ConfigManager;
import com.hyjx.framework.service.RightManager;
import com.hyjx.framework.service.SessionManager;
import com.hyjx.framework.service.UserCenterService;
import com.hyjx.framework.uc.user.UserInfo;
import com.hyjx.framework.uc.user.UserModel;
import org.jasig.cas.client.validation.Assertion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 */
@Configuration
public class UserInterceptor implements HandlerInterceptor {
    Logger log = LoggerFactory.getLogger(UserInterceptor.class);
    
    
    /**
     * 身份验证的拦截事件，若存在session，则为已登录；可配置例外
     *
     * @param request
     * @param response
     * @param obj
     * @return
     *
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
        String casEnabled = PropertyUtil.getValue("hyjx.cas.enabled");
        if ("true".equalsIgnoreCase(casEnabled)) {
            HttpSession session = request.getSession(false);
            Assertion assertion = session != null ? (Assertion) session.getAttribute("_const_cas_assertion_") : null;
            if (assertion != null) {
                LoginService loginService = (LoginService) SpringBeanUtil.getBean(Constants.LOGIN_SERVICE_NAME);
                if (loginService.isLogined()) {
                    return true;
                } else {
                    this.log.info(StringHelper.getDateTimeMill() + " 单点登陆校验成功开始登录");
                    UserModel userModel = RightManager.getUserByAccount(assertion.getPrincipal().getName());
                    UserInfo ui = new UserInfo();
                    ui.setUser_id(userModel.getUser_id());
                    SessionManager.set("_USERINFO_", ui);
                    SessionManager.set("subsys_id", PropertyUtil.getValue("hyjx.cas.defaultSubSys"));
                    request.setAttribute("subsys", RightManager.getAllSubsys());
                    this.log.info(StringHelper.getDateTimeMill() + " 登录成功");
                    loginService.initLogined();
                    loginService.afterLogin();
                    return true;
                }
            }
        }
        
        String uri = request.getRequestURI();
        String ignoreStatic = "/images|/js|/css|/theme|";
        try {
            String ignorePattern = ConfigManager.getConfig("userIntereptor.ignorePattern");
            Pattern p = Pattern.compile(ignoreStatic + ignorePattern);
            Matcher m = p.matcher(uri);
            if (m.find()) {
                return true;
            }
        } catch (NullPointerException ex) {
            log.warn(uri);
        }
        
        
        UserInfo ui = UserCenterService.getCurrentUserInfo();
        
        if (ui == null) {
            String jSessionId = getJSessionId(request);
            LoginService loginService = (LoginService) SpringBeanUtil.getBean(Constants.LOGIN_SERVICE_NAME);
            Object userObj = CacheService.get(jSessionId);
            String logoutAction = loginService.logout();
            if (userObj != null) {
                logoutAction = loginService.getLogoutByUser((UserInfo) userObj);
                CacheService.remove(jSessionId);
            }
            
            String XRequested = request.getHeader("x-requested-with");
            if ("XMLHttpRequest".equals(XRequested)) {
                //ajax request
                response.getWriter().write("sessionInvalid");
            } else {
                String path = request.getContextPath();
                String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
                String msg = "<script>location.href='" + basePath + logoutAction + ".xhtml" + "';</script>";
                response.getWriter().write(msg);
            }
            return false;
        }
        return true;
    }
    
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        
    }
    
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        
    }
    
    /**
     * 取当前JSessionId
     *
     * @return
     */
    private String getJSessionId(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        if (cookies == null) {
            return "";
        }
        
        String jSessionId = "";
        for (int i = 0; i < cookies.length; i++) {
            Cookie cook = cookies[i];
            if (Constants.JSESSIONID.equals(cook.getName())) { //获取键
                jSessionId = cook.getValue().toString();
                break;
            }
        }
        return jSessionId;
    }
}